package cn.yunhe.intercept;

import cn.yunhe.entity.Log;
import cn.yunhe.entity.User;
import cn.yunhe.service.LogService;
import cn.yunhe.util.DateUtil;
import cn.yunhe.util.IpUtil;
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Configuration
@Aspect
public class LogIntercept {

    @Resource
    private LogService logService;

    private static User user=new User();
    private static String ip;

    @Pointcut("execution(* cn.yunhe.service.*.*(..)) && !execution(* cn.yunhe.service.LogService.*(..))")
    public void pointCut(){

    }

    @AfterReturning("execution(* cn.yunhe.controller.UserController.userLogin(..))")
    public void logLogin(){
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        HttpSession session =request.getSession();
        // result的值就是被拦截方法的返回值
        if (session.getAttribute("Curr_User")!=null){
            ip=IpUtil.getIpAddress(request);
            user= (User) session.getAttribute("Curr_User");
            Log log = new Log();
            log.setUsername(user.getUserName());
            log.setUserDo("用户 登录 操作");
            log.setUserIp(ip);
            log.setLogTime(DateUtil.DateTime2String(new Date()));
            logService.addLog(log);
        }


    }
    @AfterReturning(value = "pointCut()",returning = "object")
    public void logDo(JoinPoint joinPoint,Object object){
        if (user.getUserName()!=null && user.getUserName()!="") {
            String methodName = joinPoint.getSignature().getName();
            String className = joinPoint.getTarget().getClass().getName();
            String temp = className.substring(22, 27);
            String insertClassName = getInsertClassName(temp);
            String temp2 = methodName.substring(0, 3);
            String insertMethodName = getInsertMethodName(temp2);
            Log log = new Log();
            log.setUsername(user.getUserName());
            log.setUserIp(ip);
            log.setLogTime(DateUtil.DateTime2String(new Date()));
            log.setUserDo(insertClassName + " 位置, 执行了: " + insertMethodName +" 操作");
            logService.addLog(log);
        }
    }

    private static String getInsertMethodName(String str){
        if(str.equals("get") || str.equals("que") || str.equals("sel") || str.equals("fin")){
            return "查询";
        }
        if(str.equals("del") || str.equals("rem")){
            return "删除";
        }
        if(str.equals("upd") || str.equals("mod")){
            return "修改";
        }
        if(str.equals("add") || str.equals("ins")){
            return "插入";
        }else {
            return "其他";
        }
    }

    private static String getInsertClassName(String insertClassName){
        if(insertClassName.equals("Depar")){
            return "班级";
        }
        if(insertClassName.equals("ExamP")){
            return "试卷";
        }
        if(insertClassName.equals("ExamS")){
            return "考试";
        }
        if(insertClassName.equals("Field")){
            return "科目";
        }
        if(insertClassName.equals("Knowl")){
            return "知识分类";
        }
        if(insertClassName.equals("NewsS")){
            return "公告";
        }
        if(insertClassName.equals("Quest")){
            return "试题";
        }
        if(insertClassName.equals("TagSe")){
            return "标签";
        }
        if(insertClassName.equals("UserS")){
            return "用户";
        }else {
            return "其他";
        }
    }

    public LogService getLogService() {
        return logService;
    }

    public void setLogService(LogService logService) {
        this.logService = logService;
    }
}
